package com.molyfun.parents.activity.report.iepPlan;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.report.adapter.PlanDetailTabAdapter;
import com.molyfun.parents.impl.PlanDetailTabCallback;
import com.molyfun.parents.impl.ScrollViewListener;
import com.molyfun.parents.rest.bean.GetIepPlanTableResponse;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.view.ObservableScrollView;

import java.util.Calendar;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/13/16.
 */
public class IEPPlanView extends LinearLayout implements PlanDetailTabCallback {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.product_appointment)
    TextView mProductAppointment;
    @InjectView(R.id.recyclerViewLeft)
    RecyclerView mRecyclerViewLeft;
    @InjectView(R.id.right_container_layout)
    LinearLayout mRightContainerLayout;
    @InjectView(R.id.product_num)
    TextView mProductNum;

    @InjectView(R.id.scrollview)
    ObservableScrollView mObservableScrollView;

    private Calendar mCalendar;

    private PlanDetailTabAdapter mProductDetailTabAdapter;

    //右侧table列表
    private GetIepPlanTableResponse.IEPPlanTableBean mIEPPlanTableBean;
    LinearLayoutManager mLinearLayoutManager;

    public IEPPlanView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public IEPPlanView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public IEPPlanView(Context context) {
        super(context);
        init(context, null);
    }


    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_iep_plan, this);
        ButterKnife.inject(this);
        mCalendar = Calendar.getInstance();
    }

    public void setListScrollListener(ScrollViewListener listScrollListener){
        mObservableScrollView.setScrollViewListener(listScrollListener);
    }

    public void initData(GetIepPlanTableResponse.IEPPlanTableBean iepPlanTableBean) {

        if (iepPlanTableBean == null) {
            return;
        }
        mIEPPlanTableBean = iepPlanTableBean;

        mProductAppointment.setText(TimeUtils.getDateTileMonthByTimeMillis(iepPlanTableBean.getBegindatetime()) + "~" + TimeUtils.getDateTileMonthByTimeMillis(iepPlanTableBean.getEnddatetime()));

        if(iepPlanTableBean.getIeptemplist() != null){
            mProductNum.setText(iepPlanTableBean.getIeptemplist().size() + "");
        }else{
            mProductNum.setText("0");
        }
        mLinearLayoutManager = new LinearLayoutManager(context);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerViewLeft.setLayoutManager(mLinearLayoutManager);
        mRecyclerViewLeft.setHasFixedSize(true);

        mProductDetailTabAdapter = new PlanDetailTabAdapter(context, this);
        mRecyclerViewLeft.setAdapter(mProductDetailTabAdapter);

        mProductDetailTabAdapter.refreshData(iepPlanTableBean.getIeptemplist());

        onPlanDetailTabClick(0);

    }


    @Override
    public void onPlanDetailTabClick(int position) {
        if(mIEPPlanTableBean.getIeptemplist().size() == 0){
            return;
        }
        GetIepPlanTableResponse.IEPPlanTableBean.IeptemplistBean ieptemplistBean = mIEPPlanTableBean.getIeptemplist().get(position);
        mRightContainerLayout.removeAllViews();
        if(ieptemplistBean.getIeptemplist() != null && ieptemplistBean.getIeptemplist().size() > 0){

            for(int count = 0; count < ieptemplistBean.getIeptemplist().size(); count ++){
                IEPPlanCellListView iepPlanCellListView = new IEPPlanCellListView(getContext());
                iepPlanCellListView.initData(ieptemplistBean.getIeptemplist().get(count), count == 0);
                mRightContainerLayout.addView(iepPlanCellListView);
            }
        }

    }
}